package com.tg.app.activity.device.settings;


import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.base.BaseActivity;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.app.adapter.DeviceShareAdapter;
import com.tg.data.http.entity.CancelShareBean;
import com.tg.data.bean.DeviceSettingsInfo;
import com.tange.core.backend.service.http.ClientObserver;
import com.tg.network.socket.http.TGHttp;
import com.tg.data.http.entity.ShareUser;
import com.appbase.custom.constant.CommonConstants;

import com.tg.app.R;
import com.tg.appcommon.android.TGAlertDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class DeviceShareListActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private List<ShareUser> shareUsers = new ArrayList<>();

    private DeviceShareAdapter adapter;
    private LinearLayout llNoData;
    private long deviceId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_device_share_list);
        hideActionBar();
        initView();
        DeviceSettingsInfo info = getIntent().getParcelableExtra(DeviceSettingsActivity.EXT_DEVICE_INFO);
        deviceId = info.deviceID;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.rv_device_share_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this,RecyclerView.VERTICAL));
        llNoData = findViewById(R.id.ll_device_share_no_data);
        findViewById(R.id.device_share_list_add_btn).setOnClickListener(view -> {
            Intent intent = new Intent(DeviceShareListActivity.this, ShareActivity.class);
            intent.putExtra(CommonConstants.EXT_DEVICE_ID, deviceId);
            startActivity(intent);
        });
        TextView titleView = findViewById(R.id.device_name);
        titleView.setText(R.string.share_list);
        findViewById(R.id.back_toolbar).setOnClickListener(view -> finish());

    }

    @Override
    protected void onResume() {
        super.onResume();
        setupData();
    }

    private void setupData() {
        adapter = new DeviceShareAdapter(this, shareUsers);
        recyclerView.setAdapter(adapter);
        getDataList();
        adapter.setOnDeleteDeviceClickListener(new DeviceShareAdapter.onDeleteDeviceClickListener() {
            @Override
            public void onDelete(int position) {
                showDialo(position);
            }
        });
    }

    private void getDataList() {
        showLoading();
        HashMap<String,String> map = new HashMap<>();
        map.put("device_id",String.valueOf(deviceId));
        TGHttp.getInstance().getShareList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<List<ShareUser>>() {

                    @Override
                    protected void onSuccess(List<ShareUser> content) {
                        shareUsers.clear();
                        if (content != null ) {
                            shareUsers.addAll(content);
                        }
                        if (shareUsers != null && shareUsers.size() > 0) {
                            recyclerView.setVisibility(View.VISIBLE);
                            llNoData.setVisibility(View.GONE);
                            adapter.notifyDataSetChanged();
                        } else {
                            recyclerView.setVisibility(View.GONE);
                            llNoData.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {
                        if (errorCode == 50721) {
                            showToast(R.string.deviceid_empty);
                        } else if (errorCode == 50723) {
                            showToast(R.string.device_notyou);
                        }
                    }

                    @Override
                    protected void onOtherError(String error) {

                    }

                    @Override
                    protected void onFinish() {
                        hideLoading();
                    }
                });
    }
    //18681540542
    private void deleteDevice(ShareUser user, final int position) {
        if (user == null) return;
        String account;
        HashMap<String, String> map = new HashMap<>();
        if (!TextUtils.isEmpty(user.getPhone())) {
            account = user.getPhone();
        } else {
            account = user.getEmail();
        }
        map.put("account", account);
        map.put("device_id", String.valueOf(deviceId));
        showLoading();
        TGHttp.getInstance().cancelShare(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<List<CancelShareBean>>() {
                    @Override
                    protected void onSuccess(List<CancelShareBean> content) {
                        shareUsers.remove(position);
                        if (shareUsers != null && shareUsers.size() > 0) {
                            recyclerView.setVisibility(View.VISIBLE);
                            llNoData.setVisibility(View.GONE);
                            adapter.notifyDataSetChanged();
                        } else {
                            recyclerView.setVisibility(View.GONE);
                            llNoData.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {

                    }

                    @Override
                    protected void onOtherError(String error) {

                    }

                    @Override
                    protected void onFinish() {
                        hideLoading();
                    }
                });
    }

    public void showDialo(final int position) {
        ShareUser user = shareUsers.get(position);
        String account;
        if (!TextUtils.isEmpty(user.getPhone())) {
            account = user.getPhone();
        } else {
            account = user.getEmail();
        }

        new TGAlertDialog(this).builder().setMessage(String.format(getString(R.string.delete_share_tip), account)).setPositiveButton(R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteDevice(shareUsers.get(position), position);
            }
        }).setNegativeButton(getString(R.string.cancel), new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }).show();
    }
}
